package com.example.javachatroomextend.controller;

import com.example.javachatroomextend.model.Result;
import com.example.javachatroomextend.model.Session;
import com.example.javachatroomextend.model.User;
import com.example.javachatroomextend.service.SessionService;
import com.example.javachatroomextend.service.TodoMessageService;
import com.example.javachatroomextend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/session")
public class SessionController {
    @Autowired
    private SessionService sessionService;

    @Autowired
    private UserService userService;

    @Autowired
    private TodoMessageService todoMessageService;

    @GetMapping("/getSessionList")
    public Result<List<Session>> getSessionList(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute("user");
        List<Integer> sessionIds = sessionService.getSessionListByUserId(user.getUserId());
        List<Session> ret = new ArrayList<>();
        for (Integer n : sessionIds) {
            Session session1 = sessionService.getSessionBySessionId(n);
            session1.setSessionId(n);
            Integer friendId = sessionService.getFriendUserIdBySessionId(n, user.getUserId());
            User friend = userService.getUserByUserId(friendId);
            session1.setFriendName(friend.getUsername());
            session1.setImage(friend.getImage());
            ret.add(session1);
        }
        ret.sort((a,b) -> b.getLastTime().compareTo(a.getLastTime()));
        return Result.success(ret);
    }

    @Transactional
    @PostMapping("/createSession")
    public Result<Session> createSession(String friendName,HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute("user");
        Session session1 = new Session();
        User user1 = userService.getUserByName(friendName);
        session1.setImage(user1.getImage());
        sessionService.createSession(session1);
        sessionService.addSessionUser(session1.getSessionId(), user.getUserId());
        sessionService.addSessionUser(session1.getSessionId(),user1.getUserId());
        todoMessageService.createTodoMessage(user1.getUserId(), session1.getSessionId());
        return Result.success(session1);
    }

    @GetMapping("/getSessionIdByUsername")
    public Result<List<Integer>> getSessionIdByUsername(String username,HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute("user");
        List<Integer> ret = sessionService.getSessionIdByUserName(username, user.getUserId());
        return Result.success(ret);
    }
}
